package com.wocao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wocao.entity.*;
import com.wocao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 房屋绑定记录表  前端控制器
 * </p>
 *
 * @author gyh
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/zyOwnerRoomRecord")
public class ZyOwnerRoomRecordController {

    @Autowired
    private ZyOwnerRoomRecordService zyOwnerRoomRecordService;

    @Autowired
    private ZyCommunityService zyCommunityService;

    @Autowired
    private ZyBuildingService zyBuildingService;

    @Autowired
    private ZyUnitService zyUnitService;

    @Autowired
    private ZyRoomService zyRoomService;

    @GetMapping("ownerRoomRecords/{ownerId}")
    public ResponseResult<ZyOwnerRoomRecord> findRoomsByBuildingId(@PathVariable String ownerId) {
        try {
            List<ZyOwnerRoomRecord> ownerRoomRecordList = zyOwnerRoomRecordService.list(new QueryWrapper<ZyOwnerRoomRecord>().eq("owner_id", ownerId));

            for (ZyOwnerRoomRecord record : ownerRoomRecordList) {
                //格式化创建时间
                LocalDateTime createTime = record.getCreateTime();

                String formattedCreateTime = createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                record.setFormattedCreateTime(formattedCreateTime);

                //格式化修改时间
                LocalDateTime updateTime = record.getUpdateTime();

                if (updateTime !=null){
                    String formattedUpdateTime = updateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                    record.setFormattedUpdateTime(formattedUpdateTime);
                }

                //绑定地址
                StringBuilder address = new StringBuilder();
                //根据id获取房屋
                ZyRoom room = zyRoomService.getById(record.getRoomId());
                //获取小区名称
                String communityName = room.getCommunityName();
                address.append(communityName);
                //获取楼栋名称
                String buildingName = room.getBuildingName();
                address.append(buildingName);
                //获取单元名称
                String unitName = room.getUnitName();
                address.append(unitName);
                //获取房屋名称
                String roomName = room.getRoomName();
                address.append(roomName);

                record.setAddress(address.toString());

            }
            return ResponseResult.ok(ownerRoomRecordList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, "查询失败:" + e.getMessage());
        }
    }
}
